package pl.edu.pw.perg.compinfodb.ui;

import java.awt.Color;

import javax.swing.event.TableModelEvent;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.ColorHighlighter;

import pl.edu.pw.perg.compinfodb.datamodel.CableDetailsTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.ElementTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.HistoryTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.SearchTableModel;

public class CustomRendererTableFactory {

	private ColorRenderer renderer = new ColorRenderer();

	public CustomRendererTableFactory() {

	}

	@SuppressWarnings("serial")
	public JXTable getTable(TableModel tm) {
		JXTable table = new JXTable(tm) {
			// @Override
//			public void tableChanged(TableModelEvent e) {
				// super.tableChanged(e);
				// // for (int i = 0; i < getColumnCount(); i++) {
				// // getColumnModel().getColumn(i).setCellRenderer(
				// // renderer);
				// // }
//				packAll();
//			}

			//			

			@Override
			public TableCellRenderer getCellRenderer(int row, int column) {
				return renderer;
			}

			@Override
			public int getSelectedRow() {

				return super.getSelectedRow();
			}
		};

		// for (TableColumn column : table.getColumns()) {
		// column.setCellRenderer(renderer);
		// }

		// ColorHighlighter highlighters = new ColorHighlighter();
		// highlighters.setBackground(Color.YELLOW);
		// highlighters.setHighlightPredicate(new RowHighlightPredicate(2));
		// table.setHighlighters(highlighters );

		table.packAll();
		// table.setColumnControlVisible(true);
		// table.setAutoCreateColumnsFromModel(false);
		// table.setAutoCreateRowSorter(true);
		return table;
	}

	public JXTable getElementTable(ElementTableModel tm) {
		JXTable table = getTable(tm);
		JXSelectionModel selModel = new JXSelectionModel(table);
		table.setSelectionModel(selModel);
		table.getSelectionModel().addListSelectionListener(tm);
		table.addFocusListener(tm);
		return table;
	}

	public JXTable getElementTable(CableDetailsTableModel cableDetailsTableModel) {
		JXTable table = getTable(cableDetailsTableModel);
		JXSelectionModel selModel = new JXSelectionModel(table);
		table.setSelectionModel(selModel);
		selModel.addListSelectionListener(cableDetailsTableModel);
		return table;
	}

	public JXTable getSearchTable(SearchTableModel stm) {
		return new JXTable();
	}

	public JXTable getHistoryTable(HistoryTableModel htm) {
		JXTable table = getTable(htm);
		table.addFocusListener(htm);
		htm.addTableModelListener(table);
		return table;
	}
}
